﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using ModulesNS = BuXiaChu.OrderDishes.Web.Admin.Modules.Sys;
using BuXiaChu.OrderDishes.BLL;
namespace BuXiaChu.OrderDishes.Web.Admin.Employees
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Employees : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);
            var type = Request.Form["t"];
            switch (type)
            {
                case "1":
                    GetList();
                    break;
                case "4":
                    Delete();
                    break;
                default: break;
            }

        }

        private void GetList()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                var pageCount = 0;
                var pageIndex = 0;
                var pageSize = 0;
                GetPageBarParams(out pageIndex, out pageSize);
                var sqlWhere = GetSearchCondition();
                UserControlPreviewManager<ModulesNS.EmployeesList> preivewManager = new UserControlPreviewManager<ModulesNS.EmployeesList>();
                ModulesNS.EmployeesList userControl = preivewManager.LoadUserControl("~/Modules/Sys/EmployeesList.ascx");
                userControl.PageDataList = EmployeesManager.Search(sqlWhere, pageIndex, pageSize, out pageCount);
                userControl.PageIndex = pageIndex;
                userControl.PageCount = pageCount;
                result.message = preivewManager.RenderHtml(userControl);
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }
            SerializeDataToResponseStream(result);
            #endregion
        }

        private void Delete()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            var id = GetFormValue("id");
            var iId = 0;
            if (int.TryParse(id, out iId))
            {
                result.success = EmployeesManager.Delete(iId);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        private string GetSearchCondition()
        {
            var sqlWhere = " 1=1 ";
            var name = GetFormValue("name");
            if (string.IsNullOrEmpty(name) == false)
            {
                sqlWhere += " and name like'%" + name.Trim() + "%'";
            }

            var city = GetFormValue("cityId");
            var cityId = 0;
            if (string.IsNullOrEmpty(city)==false
                && int.TryParse(city, out cityId)
                && cityId > 0)
            {
                sqlWhere += " and cityid=" + city.Trim();
            }

            var jobTitle = GetFormValue("jobTitle");
            var iJobTitle = 0;
            if (int.TryParse(jobTitle, out iJobTitle) && iJobTitle>0)
            {
                sqlWhere += " and jobtitle = " + jobTitle;
            }

            return sqlWhere;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
